//
//  QuarterView.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/1/29.
//  Copyright © 2016年 game. All rights reserved.

#import "QuarterView.h"
#import "QuartModel.h"

@interface QuarterView ()<UIScrollViewDelegate>

@end

@implementation QuarterView


-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        
        self.scrollview=[[UIScrollView alloc] initWithFrame:CGRectMake(0,0,frame.size.width, frame.size.height)];
        self.scrollview.showsHorizontalScrollIndicator=NO;

//        self.scrollview.pagingEnabled=YES;
        self.scrollview.bounces = NO;
        [self addSubview:self.scrollview];
        self.scrollview.backgroundColor=[UIColor clearColor];
        self.scrollview.delegate=self;
        
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, H(self)-0.7, SCREEN_WIDTH, 0.7)];
        view.backgroundColor = [UIColor lightGrayColor];
        view.alpha = 0.8;
        [self addSubview:view];
    
        
        self.backgroundColor = [UIColor whiteColor];
        
    }
    return self;
    

}

-(void)setDataArry:(NSMutableArray *)dataArry{
    
    _dataArry = dataArry;
    CGFloat xd = 10;//横间隙；
    CGFloat yd = 10;//竖间隙

    CGFloat width = (SCREEN_WIDTH-xd*5)/4;
    CGFloat hight = H(self)-2*yd;
    
    self.scrollview.contentSize=CGSizeMake((self.dataArry.count+1)*(width+xd)+10, 0);

    for (int i=0;i<_dataArry.count+1;i++ ) {
        
        UIButton *bnt = [UIButton buttonWithType:UIButtonTypeSystem];
        
        bnt.layer.borderColor = RGBA(220, 220, 220, 1).CGColor;
    
        if (i==0) {
            [bnt setTitle:@"最新" forState:UIControlStateNormal];
           
            [bnt setBackgroundImage:[UIImage imageNamed:@"icon-Select"] forState:UIControlStateNormal];
            bnt.layer.borderColor = [UIColor clearColor].CGColor;
            bnt.tintColor = mainColor;
            
        }else{
             QuartModel *model = _dataArry[i-1];
  

                [bnt setTitle:model.title forState:UIControlStateNormal];
                bnt.backgroundColor = [UIColor whiteColor];
                [bnt setTitleColor:RGBA(46,46,46,0.8) forState:UIControlStateNormal];
        }
        
        bnt.tag = i+10;
        
        bnt.titleLabel.font = font(SCREEN_WIDTH*0.05);
        
        [bnt addTarget:self action:@selector(didSender:) forControlEvents:UIControlEventTouchUpInside];
    
        int row = i/10;
        int colum = i%10;

        CGFloat x = xd+(width + xd)*colum;
        CGFloat y = yd+(hight+yd)*row;
        bnt.frame = CGRectMake(x,y,width, hight);
        
        bnt.layer.borderWidth = 1;
        
        bnt.layer.masksToBounds = YES;
        radius(bnt,2);
        [self.scrollview addSubview:bnt];
    }

}

-(void)didSender:(UIButton *)button{
    
    /**
     *  设置点击时Scrollview的偏移
     * 当数组 _dataArry 的个数大于三 又往里面添加了一个“最新”  就等于当总数大于5个标签的时候 才设置偏移 屏幕每次放4个
     * 第0个标签不设置偏移  第1个偏移0个标签的宽度 第2个标签偏移1个标签的宽度--(button.width+10) 以此往后推 目的是让选中的标签处在屏幕的第二个位置
     * 当点击最后两个标签时  偏移位置跟倒数第3个的偏移位置一样 保证屏幕始终显示4个标签
     */
    NSInteger index = button.tag-10;
    if (_dataArry.count>3&&index>=1) {
        [self.scrollview setContentOffset:CGPointMake((button.width+10)*(index-1), 0) animated:YES];
        
        if (index==_dataArry.count||index==_dataArry.count-1) {//最后一个跟倒数第二个
      
             [self.scrollview setContentOffset:CGPointMake((button.width+10)*(_dataArry.count-3), 0) animated:YES];
        }
    }
    
    for (int i=0; i<_dataArry.count+1; i++) {
        
        UIButton *Btn=(UIButton*)[self viewWithTag:10+i];
        
        if (Btn.tag==button.tag) {
        
            [Btn setBackgroundImage:[UIImage imageNamed:@"icon-Select"] forState:UIControlStateNormal];
            Btn.layer.borderColor = [UIColor clearColor].CGColor;
      
            [Btn setTitleColor:mainColor forState:UIControlStateNormal];

        }else{
            [Btn setBackgroundImage:[UIImage imageNamed:@"holk"] forState:UIControlStateNormal];
            
            Btn.backgroundColor = [UIColor whiteColor];
            
            [Btn setTitleColor:RGBA(46,46,46,0.8) forState:UIControlStateNormal];
            Btn.layer.borderColor = RGBA(220, 220, 220, 1).CGColor;
        }
    }


    if (button.tag==10) {
        [self.deleget Allclick];
  
    }else{
        QuartModel *model = _dataArry[button.tag-11];
        [self.deleget QuarClick:model.article_list_url];
   
    }

}

@end
